import { history } from 'umi';
import { Input, Select, DatePicker, Modal, Checkbox, Upload } from 'antd';
import { ExclamationCircleOutlined } from '@ant-design/icons';
const { TextArea } = Input;
const { confirm } = Modal;
export function goBack() {
  history.goBack();
}

export const paginationOptions = {
  //   current: 1,
  //   pageSize: 10,
  showQuickJumper: true,
  //   itemRender(current, type, originalElement) {
  //     if (type === 'prev') {
  //       return '上一页';
  //     }
  //     if (type === 'next') {
  //       return '下一页';
  //     }
  //     return originalElement;
  //   },
};

export function transFormIndex(pagination, index) {
  return (
    ((pagination.current || 1) - 1) * (pagination.pageSize || 10) + index + 1
  );
}
export const checkStatusMap = (key) => {
  const arr = ['未读', '已读未处置', '已处置'];
  return arr[key - 1] || arr[0];
};
export const reportStatusMap = (key) => {
  const arr = ['草稿', '已部署'];
  return arr[key] || arr[0];
};
export const pressStatusMap = (key) => {
  const arr = ['未处置', '已处置'];
  return arr[key] || arr[0];
};

export const componentsMap = (type, props = {}, readOnly = false) => {
  if (!readOnly) {
    props.placeholder =
      !type || type === 'input' || type === 'textarea' ? '请输入' : '请选择';
  } else {
    props.placeholder = '无';
  }

  props.readOnly = readOnly;
  if ((type === 'select' || type === 'datepicker') && readOnly) {
    props.disabled = true;
  }
  if (type === 'datepicker') {
    props.inputReadOnly = true;
    props.getPopupContainer = (triggerNode) =>
      triggerNode.parentNode || document.body;
  }
  const typeList = {
    input: <Input {...props}></Input>,
    select: <Select {...props}></Select>,
    datepicker: <DatePicker {...props}></DatePicker>,
    textarea: <TextArea {...props}></TextArea>,
    checkbox: <Checkbox.Group {...props}></Checkbox.Group>,
    upload: <Upload {...props}></Upload>,
  };
  return typeList[type] || typeList.input;
};

export function showDeleteConfirm(ok, text = '删除') {
  confirm({
    title: '是否确定删除?',
    icon: <ExclamationCircleOutlined />,
    content: '删除后将不可恢复',
    okText: '确定',
    okType: 'danger',
    cancelText: '取消',
    onOk: ok,
    onCancel() {},
  });
}

export const download = (filename, link) => {
  let DownloadLink = document.createElement('a');
  DownloadLink.style = 'display: none'; // 创建一个隐藏的a标签
  DownloadLink.download = filename;
  DownloadLink.href = link;
  document.body.appendChild(DownloadLink);
  DownloadLink.click(); // 触发a标签的click事件
  document.body.removeChild(DownloadLink);
};

export function GetRequest() {
  const url = location.search;

  //获取url中"?"符后的字串

  const theRequest = {};

  if (url.indexOf('?') != -1) {
    const str = url.substr(1);

    const strs = str.split('&');

    for (let i = 0; i < strs.length; i++) {
      theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1]);
    }
  }
  return theRequest;
}

export const checkPhone = async (rule, value) => {
  if (!value) {
    return Promise.reject('请输入手机号码');
  } else {
    if (!/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(value)) {
      return Promise.reject('请输入正确的手机号码');
    } else {
      return Promise.resolve();
    }
  }
};

export const checkLandPhone = async (rule, value) => {
  if (!value | /^0\d{2,3}-\d{7,8}/.test(value)) {
    return Promise.resolve();
  } else {
    return Promise.reject('请输入正确的座机号码,如：0618-83945008');
  }
};
